home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / WIN / VB_CTRLS / MHSAMPL.ZIP / MHSAMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-14  |  13.5 KB  |  442 lines

  1. //---------------------------------------------------------------------------
  2. // MhSample.c
  3. //---------------------------------------------------------------------------
  4. /* 
  5. ---------------------------------------------------------------------------
  6.      Function : MicroHelp, Inc. Sample Custom Control
  7. ---------------------------------------------------------------------------
  8.      Properties :
  9.             Standard Properties (see VB documentation)
  10.      CtlName         Enabled         Height          Index
  11.      Left            Parent          Tag             Top
  12.      Width           BackColor
  13.  
  14.             Non-Standard Properties
  15. ---------------------------------------------------------------------------
  16.      Events :
  17.             Standard Events (see VB documentation)
  18.             None
  19.             Non-Standard Events
  20. '----------------------------------------------------------------------------
  21.      Methods :
  22. ============================================================================*/
  23.  
  24. #define NOCOMM
  25. #include <windows.h>     //Standard windows.h
  26. #include <vbapi2.h>      //VB API
  27. #include "mhSample.h"    //VB2+ Control Model
  28. #include "mhSampl1.h"    //VB1  Control Model
  29.  
  30. //Definition of structure used when calling VBFireEvent - 
  31. //Note:  This structure varies based on the type and number
  32. //       of paramaters passed to each event.
  33.  
  34. typedef struct tagPARAMS // event procedure parameter profiles
  35. {
  36.      WORD FAR   *lpwp2;  // Integer Parameter #2
  37.      WORD FAR   *lpwp1;  // Integer Parameter #1
  38.      LPVOID      Index;  // reserve space for index parameter to array ctl
  39. }
  40. PARAMS;
  41.  
  42. PARAMS params;
  43.  
  44. HANDLE hmodDLL = (HANDLE) NULL;
  45.  
  46. //---------------------------------------------------------------------------
  47. // MhSample Control Message Processing function
  48. // VB Calls this procedure with each message intended for this control
  49. // This is very similar to the "normal" windows message procedure except
  50. // that it adds the HCTL (handle to control) as a parameter.
  51. //---------------------------------------------------------------------------
  52.  
  53. LONG _export MhSampleCtlProc
  54. (
  55.      HCTL        hctl,
  56.      HWND        hwnd,
  57.      USHORT      msg,
  58.      USHORT      wp,
  59.      LONG        lp
  60. )
  61. {
  62.      LONG        lResult;
  63.      PMHSAMPLE pMhSample = MHSAMPLEDEREF(hctl);  //Dereference the PDS so we have
  64.                                                                                              //a pointer to our PDS
  65.      // Message pre-processing
  66.      switch( msg )
  67.      {
  68.              
  69.             //This is the best message to set default property values
  70.             //This is because this message gets generated before a load 
  71.             //from the formfile happens.  On the initial creation of a
  72.             //control the default values set in the NCCREATE message
  73.             //will not be overwritten by a formfile load (because there
  74.             //is not a FormFile load on newly created controls.)
  75.                 
  76.             case WM_NCCREATE:
  77.             
  78.             pMhSample = MHSAMPLEDEREF(hctl);       //Dereference the PDS so we have
  79.                                                                                          //a pointer
  80.             //Set default values
  81.             pMhSample->DefaultProperty = 10;       //Sets .DefaultProperty to 10 on
  82.                                                                                          //initial creation of control
  83.             break;
  84.  
  85. /* This is a sample of how to fire an event in vb.  This also shows the
  86.      use of the params structure.  Here we are using a User-Defined
  87.      message.  User-defined messages can come in handy when firing an event
  88.      because you can use SendMessage/PostMessage from anywhere in the DLL to
  89.      fire your event (or do other processing and fire your event).   
  90.      
  91.             #define MH_USERMESSAGE = WM_USER + 1
  92.             
  93.             case MH_USERMESSAGE:
  94.             {
  95.                         
  96.                 pMhSample = MHSAMPLEDEREF(hctl);
  97.  
  98.                 hwndglobal = (HWND) wp;
  99.                 wpglobal = (WORD) lp;
  100.  
  101.                 paramsglobal.lpwp1=&hwndglobal;
  102.                 paramsglobal.lpwp2=&wpglobal;
  103.                         
  104.                 VBFireEvent(hctl, EVENT_SAMPLE_SAMPLECLOSED, ¶msglobal);
  105.                         
  106.                 pMhSample->DialogHwnd = (HWND) NULL;
  107.                      
  108.             }
  109.             return 0;
  110. */ 
  111.      
  112.      case WM_PAINT:
  113.      {
  114.             PAINTSTRUCT ps;
  115.             
  116.             //If wp is != NULL when coming into the WM_PAINT then use the wp 
  117.             // as a hDC and do all you paint work on that hDC.  
  118.             // This happens when a PrintForm command takes place.  On any
  119.             // paint message we are calling our Common User function to
  120.             // paint the control
  121.             
  122.             if (wp)
  123.                 MhSamplePaintControl(hctl, hwnd, (HDC) wp);
  124.             else
  125.             {
  126.                 BeginPaint(hwnd, &ps);
  127.                 MhSamplePaintControl(hctl, hwnd, ps.hdc);
  128.                 EndPaint(hwnd, &ps);
  129.             }
  130.         }  
  131.             break;
  132.  
  133.      //Standard Font properties required that we respond to the 2 Windows
  134.      //message for SetFont and GetFont.  Here we store the value of the
  135.      //font in our PDS and return it when needed.  
  136.      
  137.      case WM_SETFONT:
  138.              pMhSample = MHSAMPLEDEREF(hctl);                  //A pointer to our control data
  139.              pMhSample->hfont = (HFONT) wp;
  140.              return 0;
  141.  
  142.      case WM_GETFONT:
  143.              pMhSample = MHSAMPLEDEREF(hctl);                  //A pointer to our control data
  144.              return pMhSample->hfont;
  145.  
  146.      case WM_DESTROY:
  147.             pMhSample = MHSAMPLEDEREF(hctl);
  148.             //free any resources you may have created for this instance
  149.             //Free any memory, hpics, bmps, cursors, etc.
  150.             
  151.             break;
  152.  
  153.      case WM_RBUTTONDOWN:
  154.             if (VBGetMode()==MODE_DESIGN)
  155.             {
  156.                 //VBGetMode is a VB Api function that tells the control if VB
  157.                 //is MODE_DESIGN, MODE_RUN, or MODE_BREAK.
  158.  
  159.                 //Note:  This gives us design time R-button down.  MicroHelp
  160.                 //uses the right button at design time to do sizing of inner
  161.                 //areas and also to bring up Help on the control.  This control
  162.                 //only beeps when the right button is pressed at design time.
  163.                 MessageBeep(0);
  164.                 break;
  165.             }
  166.  
  167.      case VBM_GETPROPERTY:
  168.             switch ( wp )
  169.             {
  170.                 
  171.                 // wp is set to the property index and lp is where we
  172.                 // will store the value of the requested property.
  173.                 // Fill in lp (and cast to correct type) the property
  174.                 // information requested
  175.  
  176.                 //case IPROP_SAMPLE_HDC:
  177.                 //   *(SHORT FAR*) lp = pMhSample->hdc;
  178.                 //return 0;
  179.  
  180.             }
  181.             break;
  182.  
  183.      case VBM_CHECKPROPERTY:
  184.      {   
  185.             switch ( wp )
  186.             {
  187.                 
  188.                 // This message is used to check a property that VB may be 
  189.                 // setting automatically.  Return 0 to set property to value
  190.                 // otherwise return vb error code.
  191.                 // wp = property index and lp = data they are trying to set
  192.                 // the property to.
  193.  
  194.                 //case IPROP_SAMPLE_MAXFILESIZE:
  195.                 //   if ((SHORT) lp > 32767 || (SHORT) lp < 0)
  196.                 //      return ERR_BADPROPERTY;
  197.                 //return 0;
  198.             }
  199.         }
  200.         break;
  201.  
  202.      case VBM_SETPROPERTY:
  203.             switch ( wp )
  204.             {
  205.                 
  206.                 // Note:  The wp has the property index and
  207.                 // the lp has the value they are setting.
  208.                 // Cast the lp into your storage area or PDS
  209.                 // If it is a property array - lp will be a
  210.                 // LPDATASTRUCT
  211.                 
  212.                 //case IPROP_SAMPLE_ACTION:
  213.                 //    pMhSample = MHSAMPLEDEREF(hctl);
  214.                 //    pMhSample->Action = (SHORT) lp;
  215.                 //    break;
  216.             }
  217.             break;
  218.  
  219.      case VBM_CREATED:
  220.         //This message is fired after all properties have been loaded from the
  221.         //form file and the control is getting ready to be drawn on the screen.
  222.         //If you return 0 here then the control will not display itself at 
  223.         //run-time.  We don't wan't it to display so we are returning 0 ONLY if
  224.         //the mode is NOT= MODE_DESIGN.  This is similar to the TIMER control
  225.         //that only shows at design time.  Remove the 2 lines below to have the
  226.         //control show up at run-time.
  227.  
  228.         if (VBGetMode() != MODE_DESIGN)
  229.             return 0L;
  230.         break;
  231.  
  232.      }
  233.  
  234.      // Default processing:
  235.      // VB will process message or pass it on to Windows default processing
  236.      // This is a very important item!  All standard properties and events
  237.      // are fired based on default processing getting called.  If you "return"
  238.      // from any of the above messages then default proc DOES NOT get called.
  239.      
  240.      
  241.      lResult = VBDefControlProc( hctl, hwnd, msg, wp, lp );
  242.  
  243.      //After default processing you might want to process the message again
  244.      //to do something different.  Also you might have a need for VB to do
  245.      //the default processing and then you to do your own processing.
  246.      //This sample app doesn't use post processing so it is commented out.
  247.  
  248.      // Message post-processing:
  249.  
  250.      //switch( msg )
  251.      //{
  252.      //}
  253.  
  254.      //LResult is the result value from the message process.  This is usually
  255.      //set by default processing but it may also be returned from any message
  256.      //that you process.  For messages such as VBM_SETPROPERTY, if you return
  257.      //a value that is in the VB error code range then VB will bring up the
  258.      //appropriate Run-time error message box (or user defined error if out of
  259.      //range
  260.      
  261.      return lResult;
  262. }
  263.  
  264. VOID NEAR MhSamplePaintControl(HCTL hctl, HWND hwnd, HDC hdc)
  265. {
  266.                 RECT rect;
  267.                 HBRUSH hbr;
  268.                 HBRUSH hbrOld = (HBRUSH) NULL;
  269.                 HFONT hfontOld = (HFONT) NULL;
  270.                 PMHSAMPLE pMhSample = MHSAMPLEDEREF(hctl);
  271.                 
  272.                 //Note: To Get the standard back color property we send a message
  273.                 //to the parent requesting the CTLCOLOR of our child control.  This
  274.                 //returns us a brush of that color.  This brush is owned by the parent
  275.                 //so don't DeleteObject it!
  276.  
  277.                 hbr = (HBRUSH)SendMessage(GetParent(hwnd), WM_CTLCOLOR, hdc, MAKELONG(hwnd, 0));
  278.                 
  279.                 if (VBGetMode() == MODE_DESIGN)
  280.                 {
  281.                     pMhSample = MHSAMPLEDEREF(hctl);
  282.                     
  283.                     if (pMhSample->hfont)
  284.                         hfontOld = SelectObject(hdc, pMhSample->hfont);
  285.  
  286.                     hbrOld = SelectObject(hdc, hbr);
  287.  
  288.                     GetClientRect(hwnd, &rect);
  289.                     Rectangle(hwnd, rect.left, rect.top,rect.right,rect.bottom);
  290.                     DrawText(hdc, "MhSample Control", -1, &rect, DT_CENTER | DT_WORDBREAK | DT_VCENTER);
  291.                     
  292.                     if (hfontOld)
  293.                         SelectObject(hdc, hfontOld);
  294.  
  295.                     if (hbrOld)
  296.                         SelectObject(hdc, hbrOld);
  297.  
  298.                 }
  299. }
  300. //---------------------------------------------------------------------------
  301. // End of local procedures
  302. //---------------------------------------------------------------------------
  303.  
  304. //---------------------------------------------------------------------------
  305. // Register custom control.
  306. //  This routine is called by VB when the custom control DLL is
  307. //  loaded for use.
  308. //---------------------------------------------------------------------------
  309.  
  310.  
  311.  
  312. BOOL FAR PASCAL _export VBINITCC
  313. (
  314.     USHORT usVersion,
  315.     BOOL    fRuntime
  316. )
  317. {
  318.     // avoid warnings on unused (but required) formal parameters
  319.  
  320.     usVersion = usVersion;
  321.  
  322. // If we want to make this a run-time only VBX then we don't allow
  323. // it to be registered at design-time.  If we return FALSE VB won't
  324. // allow the control at design-time.
  325.  
  326. #ifdef RUN_TIME
  327.      if (!fRuntime)
  328.             return FALSE;
  329. #endif //RUN_TIME
  330.  
  331.     // This is where we decide to either register the Version 1 control
  332.     // or the Version 2 control.  Note:  Version 1 controls are compatible
  333.     // with VB 1.0, 2.0 and Visual C++ version 1.0.  Version 2 controls are
  334.     // only compatible with VB 2.0 and later.
  335.  
  336.     if (usVersion < VB_VERSION)
  337.         return VBRegisterModel(hmodDLL, &modelMhSample_vb1);
  338.     else
  339.         return VBRegisterModel(hmodDLL, &modelMhSample);
  340.  
  341.     return TRUE;
  342. }
  343.  
  344. //---------------------------------------------------------------------------
  345. // UnRegister custom control.
  346. //    This routine is called by VB when the custom control DLL is being
  347. //    unloaded.
  348. //---------------------------------------------------------------------------
  349. VOID FAR PASCAL _export VBTERMCC
  350. (
  351.      VOID
  352. )
  353. {
  354.      return;
  355. }
  356.  
  357.  
  358. //---------------------------------------------------------------------------
  359. // Provide custom control model information to host environment.
  360. // This host enviroment may be VB, VC or other third party products
  361. //---------------------------------------------------------------------------
  362. LPMODELINFO FAR PASCAL _export VBGetModelInfo
  363. (
  364.      USHORT usVersion
  365. )
  366. {
  367.      if (usVersion < VB_VERSION)
  368.         return &modelinfoMhSample_vb1;
  369.      else
  370.         return &modelinfoMhSample;
  371. }
  372.  
  373.  
  374. //---------------------------------------------------------------------------
  375. // Initialize library.
  376. //  This routine is called when the first client loads the DLL.
  377. //---------------------------------------------------------------------------
  378.  
  379. int FAR PASCAL LibMain
  380. (
  381.     HANDLE  hModule,
  382.     WORD    wDataSeg,
  383.     WORD    cbHeapSize,
  384.     LPSTR   lpszCmdLine
  385. )
  386. {
  387.     // avoid warnings on unused (but required) formal parameters
  388.  
  389.     wDataSeg = wDataSeg;
  390.     cbHeapSize = cbHeapSize;
  391.     lpszCmdLine = lpszCmdLine;
  392.  
  393.     //This is where we set the Global hmodDLL variable.  This is the instance
  394.     //handle to our DLL (.VBX)
  395.  
  396.     hmodDLL = hModule;
  397.  
  398.     return 1;
  399. }
  400.  
  401. #if (_MSC_VER < 610)
  402.  
  403. // C7 and QCWIN provide default a WEP
  404.  
  405. int FAR PASCAL WEP(int bSystemExit);
  406.  
  407. // For Windows 3.0 it is recommended that the WEP function reside in a
  408. // FIXED code segment and be exported as RESIDENTNAME.  This is
  409. // accomplished using the alloc_text pragma below and the related EXPORTS
  410. // and SEGMENTS directives in the .DEF file.
  411. //
  412. // Read the comments section documenting the WEP function in the Windows
  413. // 3.1 SDK "Programmers Reference, Volume 2: Functions" before placing
  414. // any additional code in the WEP routine for a Windows 3.0 DLL.
  415. //
  416. #pragma alloc_text(WEP_TEXT,WEP)
  417.  
  418. //---------------------------------------------------------------------------
  419. // WEP
  420. //  Performs cleanup tasks when the DLL is unloaded.  WEP() is
  421. //  called automatically by Windows when the DLL is unloaded (no
  422. //  remaining tasks still have the DLL loaded).  It is strongly
  423. //  recommended that a DLL have a WEP() function, even if it does
  424. //  nothing but returns success (1), as in this example.
  425. //---------------------------------------------------------------------------
  426. int FAR PASCAL WEP
  427. (
  428.     int bSystemExit
  429. )
  430. {
  431.     // avoid warnings on unused (but required) formal parameters
  432.  
  433.     bSystemExit = bSystemExit;
  434.  
  435.     return 1;
  436. }
  437.  
  438. #endif
  439.  
  440.  
  441. //---------------------------------------------------------------------------
  442.